<template>
	<view class="page">
		<view class="vipbox" v-for="(item,index) in userGradeList" :key="index"
			:style="'background-image: url('+vipList[index].bg+')'">
			<view class="price">
				￥<text style="font-size: 130rpx;">{{item.upgradeMoney}}</text>
			</view>
			<image @click="showPopup(item)" class="btn" :src="vipList[index].btn" mode=""></image>
		</view>
		<up-popup mode="bottom" custom-style="background: none" :show="show" @close="onClose">
			<view class="showCount">
				<image class="close" @click="onClose" src="../../../static/yhk2.png" mode=""></image>
				<view class="title">支付方式</view>
				<view class="cardList" v-for="(item,index) in cardList" :key="index" @click="activeCard(item,index)">
					<text :class="['cardName',index==active ? 'active' : '']">{{item.value}}</text>
					<view class="FX1"></view>
					<image v-if="index==active" src="../../../static/yhk3.png" mode=""></image>
				</view>
			</view>
		</up-popup>
	</view>
</template>

<script>
	import {
		pay
	} from '@/common/pay.js';
	export default {
		data() {
			return {
				show: false,
				active: null,
				cardList: [{
						id: 20,
						value: '微信'
					},
					{
						id: 30,
						value: '支付宝'
					}
				],
				userGradeList: [],
				selectedUserGradeId: '',
				vipList: [{
						bg: '../../../static/v1bg.png',
						price: '50',
						btn: '../../../static/v1.png'
					},
					{
						bg: '../../../static/v2bg.png',
						price: '99',
						btn: '../../../static/v2.png'
					},
					{
						bg: '../../../static/v3bg.png',
						price: '199',
						btn: '../../../static/v3.png'
					},
					{
						bg: '../../../static/v4bg.png',
						price: '999',
						btn: '../../../static/v4.png'
					}
				]
			}
		},
		onLoad() {
			this.getUserGradeList();
		},
		onShow() {},
		methods: {
			showPopup(userGrade) {
				if (userGrade.upgradeMoney <= 0) {
					return;
				}
				this.show = true
				this.selectedUserGradeId = userGrade.gradeId;
			},
			onClose() {
				this.show = false
			},
			activeCard(e, index) {
				this.active = index
				this.receivedType = e.value
				this.receivedID = e.id
				this.onClose();
				this.submit();
			},
			getUserGradeList() {
				let self = this;
				self._postBody(
					'user/grade/index', {
						appId: 10002,
						pageIndex: 1,
						pageSize: 100
					},
					function(res) {
						self.userGradeList = res.data.records;
					}
				);
			},
			submit() {
				let self = this;
				self.loading = true;
				uni.showLoading({
					title: '加载中'
				});
				let url = 'user/grade/upgradePay';
				let params = {
					gradeId: self.selectedUserGradeId,
					paySource: self.getPlatform(),
					payType: self.receivedID,
					useBalance: 0,
					useAgentMoney: 0,
				};
				self._postBody(url, params, function(res) {
					self.loading = false;
					uni.hideLoading();
					pay(res, self, self.paySuccess, self.payError);
				});
			},
			paySuccess(result) {
				let self = this;
				console.log(result);
				if (self.orderType == 30 || self.orderType == 70) {
					self.navigateBack();
					console.log('navigateBack');
				} else if (self.orderType == 60) {
					self.gotoPage('/pages/user/my_shop/my_shop', 'reLaunch');
				} else if (self.orderType == 20) {
					self.gotoPage('/pages/user/index/index');
				} else if (self.orderType == 50) {
					self.gotoPage('/pages/order/myorder', 'reLaunch');
				} else if (self.orderType == 30) {
					let pages = getCurrentPages();
					if (pages && pages.length > 1) {
						let currentPage = pages[pages.length - 2];
						if (currentPage && currentPage.route) {
							self.gotoPage(currentPage.route, 'redirect');
							return
						}
					}
					self.gotoPage('pages/order/recharge', 'redirect');
				} else {
					self.gotoPage('/pages/order/pay-success/pay-success?orderId=' + result.data.orderId, 'reLaunch');
				}
			},
			payError(result) {
				console.log(result, '1111111');
				this.navigateBack();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding-top: 28rpx;
	}

	.vipbox {
		margin: 0 20rpx;
		width: 710rpx;
		height: 390rpx;
		margin-bottom: 59rpx;
		background-size: 100% 100%;
		position: relative;

		.btn {
			position: absolute;
			left: 0;
			bottom: 0;
			width: 250rpx;
			height: 120rpx;
			z-index: 999;
		}

		.price {
			position: absolute;
			left: 43rpx;
			top: 166rpx;
			font-weight: bold;
			font-size: 48rpx;
			color: #FFFFFF;
			line-height: 60rpx;
		}
	}

	.showCount {
		background-color: #FFFFFF;
		padding: 40rpx 30rpx 174rpx 30rpx;

		.close {
			width: 30rpx;
			height: 30rpx;
			margin-left: 21rpx;
		}

		.title {
			font-weight: 400;
			font-size: 26rpx;
			color: #333333;
			text-align: center;
			padding-top: 12rpx;
			padding-bottom: 73rpx;
			width: 690rpx;
			height: 2rpx;
			border-bottom: 2rpx solid #D3D3D3;
		}

		.cardList {
			display: flex;
			align-items: center;
			padding: 48rpx 35rpx;
			border-bottom: 2rpx solid #D3D3D3;

			.cardName {
				font-weight: 300;
				font-size: 30rpx;
				color: #333333;
			}

			image {
				width: 30rpx;
				height: 30rpx;
				margin-top: 6rpx;
			}

			.active {
				font-weight: bold;
				font-size: 30rpx;
				color: #333333;
			}
		}
	}
</style>